package edu.ustb.controller;

import java.awt.print.Printable;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.alibaba.fastjson.JSON;
import com.sun.xml.internal.ws.developer.StreamingAttachment;

import edu.ustb.domain.User;
import edu.ustb.mapper.UserXiumiMapper;
import edu.ustb.result.Result;
import edu.ustb.service.UserPhoneXiumiService;
import edu.ustb.service.UserPhoneXiumiServiceImp;
import edu.ustb.service.UserXiumimaService;
import edu.ustb.service.UserXiumimaServiceImp;


@WebServlet("/user/xiumiphone.do")
public class UserPhoneXiumiController extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userPhone=request.getParameter("phone");
		String yanzhengma=request.getParameter("yanzhengma");
		String isDo=request.getParameter("isDo");
		Result result=new Result();
		
		System.out.println(userPhone);
		UserPhoneXiumiService userphonexiumi = new UserPhoneXiumiServiceImp();
		
		User user = userphonexiumi.findByPhone(userPhone);
		System.out.println(user);

		if("true".equals(isDo)&&user!=null) {
			String Yzm = userphonexiumi.creatYzm();
			int row = userphonexiumi.addYzmByPhone(Yzm,userPhone);
			result.setCode(3);
			result.setMsg("手机验证码："+Yzm);
			response.getWriter().print(JSON.toJSONString(result));
			System.out.println(JSON.toJSONString(result));
		}else{
			if(yanzhengma.equals(user.getUserYzm()))
			{
				result.setCode(1);
				result.setMsg("success");
				response.getWriter().print(JSON.toJSONString(result));
				System.out.println(JSON.toJSONString(result));
			}else {
				result.setCode(2);
				result.setMsg("fail");
				response.getWriter().print(JSON.toJSONString(result));
				System.out.println(JSON.toJSONString(result));
			}
			
		}
		
		
			
	
	}

}
